package com.bluetrum.abmate.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.bluetrum.abmate.MainActivity;
import com.bluetrum.abmate.R;
import com.bluetrum.abmate.utils.Utils;
import com.bluetrum.devicemanager.models.ABDevice;

public class ABNotificationManager {

    private static final String NOTIFICATION_CHANNEL_ID = "Bluetrum";
    private static final String NOTIFICATION_NAME = "EarBuds";

    private static final int LAUNCH_NOTIFICATION_REQUEST_CODE = 8001; // random
    private static final int LAUNCH_NOTIFICATION_ID = 8002; // random

    /**
     * 添加常驻通知栏的通知，用以启动App。
     * 显示的是未连接设备的视图。
     * @param context the context
     */
    public static void showLauncherNotification(@NonNull final Context context) {
        showLauncherNotification(context, null);
    }

    /**
     * 添加常驻通知栏的通知，用以启动App
     * @param context the context
     * @param device 蓝牙耳机设备，如果不为空，则显示已连接
     */
    public static void showLauncherNotification(@NonNull final Context context, @Nullable final ABDevice device) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // 点击通知栏启动MainActivity
        Intent intent = new Intent(context, MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        int flags = PendingIntent.FLAG_UPDATE_CURRENT;
        if (Utils.isAndroid12OrAbove()) {
            flags |= PendingIntent.FLAG_IMMUTABLE;
        }
        PendingIntent pi = PendingIntent.getActivity(context, LAUNCH_NOTIFICATION_REQUEST_CODE, intent, flags);

        Notification notification;

        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableVibration(false);
            channel.enableLights(false);
            channel.setSound(null, null);
            notificationManager.createNotificationChannel(channel);

            builder = new NotificationCompat.Builder(context, channel.getId());
        } else {
            builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
            builder.setVibrate(new long[] {0, 0, 0, 0, 0, 0, 0, 0, 0})
                    .setLights(0, 0, 0)
                    .setSound(null);
        }

        // 自定义通知视图
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_quick_launcher);
        if (device != null) {
            remoteViews.setTextViewText(R.id.content_title, device.getName());
            remoteViews.setTextViewText(R.id.content_text, context.getResources().getString(R.string.notification_earbuds_bluetrum_content));
            remoteViews.setImageViewResource(R.id.content_image, R.mipmap.earbuds_small);
        } else {
            // 显示默认信息
            remoteViews.setTextViewText(R.id.content_title, context.getResources().getString(R.string.notification_earbuds_default_title));
            remoteViews.setTextViewText(R.id.content_text, context.getResources().getString(R.string.notification_earbuds_default_content));
            remoteViews.setImageViewResource(R.id.content_image, R.mipmap.ic_launcher);
        }

        builder.setSmallIcon(R.drawable.ic_notification)
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setSilent(true)
                .setContentTitle(context.getResources().getString(R.string.notification_earbuds_default_title))
                .setContentText(context.getResources().getString(R.string.notification_earbuds_default_content))
                .setContent(remoteViews)
                .setOngoing(true)
                .setAutoCancel(false)
                .setContentIntent(pi);
        notification = builder.build();

        notificationManager.notify(LAUNCH_NOTIFICATION_ID, notification);
    }

    /**
     * 移除用以启动App的常驻通知栏的通知
     * @param context the context
     */
    public static void removeLauncherNotification(@NonNull final Context context) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(LAUNCH_NOTIFICATION_ID);
    }

}
